package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.qo.NoticeQueryObject;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.utils.RequestPermission;
import cn.wolfcode.utils.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private INoticeService noticeService ;

   @Autowired
   private IEmployeeService employeeService;

    @RequestMapping("/list")
    @RequestPermission(name="公告查询",expression = "notice:list")
    public String list(Model model , @ModelAttribute("qo") NoticeQueryObject qo){
        boolean admin = employeeService.isAdmin();
        model.addAttribute("admin",admin);

        PageInfo<Notice> pageInfo = noticeService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "notice/list" ;
    }



    @RequestMapping("/saveOrUpdate")
    @RequestPermission(name="公告新增/编辑",expression = "notice:saveOrUpdate")
    public String save(Notice notice , Long id , int currentPage){
        if (id != null){
            noticeService.update(notice);
        }else {
            noticeService.insert(notice);
        }

        return "redirect:/notice/list?currentPage="+currentPage;
    }

    //发布
    @RequestMapping("/issue")
    @RequestPermission(name="公告发布",expression = "notice:issue")
    @ResponseBody
    public JsonResult issue(Long id ,boolean issue){
        noticeService.issue(id,issue);
        if (issue){
            return new JsonResult(true,"发布成功") ;
        }else {
            return new JsonResult(true,"取消成功") ;
        }
    }

    //查看
    @RequestMapping("/show")
    @RequestPermission(name="公告查看",expression = "notice:show")
    public String show(Model model ,Long id,int currentPage){
        Notice notice = noticeService.selectById(id);
        model.addAttribute("notice",notice);
        model.addAttribute("currentPage",String.valueOf(currentPage));
        return "notice/show";
    }
}
